import java.io.InputStream;
import java.net.URL;

/**
 * Created by micocube
 * ProjectName: coding
 * PackageName: com.coding.Path
 * User: micocube
 * Email: ldscube@gmail.com
 * CreateTime: 2019/1/11下午4:01
 * ModifyTime: 2019/1/11下午4:01
 * Version: 0.1
 * Description:
 **/
public class PathUtils {
    public static void main(String[] args) {
        System.out.println(PathUtils.getRootPath());
    }

    public static String getRootPath(){
        URL resource = ClassLoader.getSystemResource("");
//        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
//        URL resource = classLoader.getResource("");
        String path = resource.getPath();
//        System.out.println("RootPath:"+path);
        return path;
    }


    public static String getRootPath(String name){
        URL resource = ClassLoader.getSystemResource("");
//        URL resource = Thread.currentThread().getContextClassLoader().getResource(name);
        String path = resource.getPath();
        return path;
    }

    public static String getClassRealPath(Class clazz){
        String rootPath = getRootPath();
        String path = rootPath + class2ClassPath(clazz);
        return path;
    }

    private static String class2ClassPath(Class clazz) {
        return clazz.getName().replace('.', '/') + ".class";
    }


    public static InputStream getClassInputStream(Class clazz){
        InputStream is = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(class2ClassPath(clazz));

        return is;
    }
}
